//
//  SingleAdPageView.m
//  ZYYD-Iphone
//
//  Created by yuzhou on 12-9-17.
//  Copyright (c) 2012年 yuzhou. All rights reserved.
//

#import "SingleAdPageView.h"
#import "UIImageView+WebCache.h"
#import "BookViewController.h"

@implementation SingleAdPageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        adImageView = [[[UIImageView alloc] initWithFrame:self.bounds] autorelease];
        adImageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:adImageView];
        
        UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)] autorelease];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)setInfoDic:(NSDictionary *)dic
{
    _infoDic = [dic retain];
    NSString *imageStr = [_infoDic objectForKey:@"image"];
    imageStr = [imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [adImageView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"ad_default"]];
}

- (void)tapAction
{
    switch ([[_infoDic valueForKey:@"type"] intValue]) {
        case 1:
        {
            BookViewController *bookViewController = [[[BookViewController alloc] initWithNibName:@"BookViewController" bundle:nil] autorelease];
            bookViewController.itemid = [_infoDic objectForKey:@"item_id"];
            bookViewController.itemname = [_infoDic objectForKey:@"item_name"];
            if (bookViewController.itemid == nil) {
                return;
            }
            UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
            [navController pushViewController:bookViewController animated:YES];
        }
            break;
        case 2:
        {
            NSString *URLString = [[_infoDic objectForKey:@"url"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            if (URLString == nil) {
                return;
            }
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLString]];
            
        }
            break;
            
        default:
            break;
    }
    
}

- (void)dealloc
{
    [_infoDic release];
    [super dealloc];
}

@end
